草庐IT

ios - 在 Swift 中从 AppDelegate 获取 ViewController 的实例

全部标签

ruby - 我如何在 Nokogiri 中获取下一个 HTML 元素?

假设我的HTML文档是这样的:NewsSomeinterestingnewshereSportsBaseballisfun!我可以使用以下代码获取标题div:require'rubygems'require'nokogiri'require'open-uri'url="mypage.html"doc=Nokogiri::HTML(open(url))doc.css(".headline").eachdo|item|putsitem.textend但我如何访问以下p标签中的内容,以便News与Someinterestingnewshere等相关? 最佳答案

ruby - 从 Curb 获取响应头

我打算从RubyonRails应用程序进行调用:c=Curl::Easy.http_post("https://example.com",json_string_goes_here)do|curl|curl.headers['Accept']='application/json'curl.headers['Content-Type']='application/json'curl.headers['Api-Version']='2.2'end响应应该有自定义header:X-Custom1:"somevalue"X-Custom2:"anothervalue"我如何遍历响应header

ruby - 使用 Ruby 获取网页的所有链接

我正在尝试使用Ruby检索网页的每个外部链接。我将String.scan与此正则表达式一起使用:/href="https?:[^"]*|href='https?:[^']*/i然后,我可以使用gsub删除href部分:str.gsub(/href=['"]/)这工作正常,但我不确定它在性能方面是否有效。这可以使用还是我应该使用更具体的解析器(例如nokogiri)?哪种方式更好?谢谢! 最佳答案 使用正则表达式对于快速而肮脏的脚本来说很好,但Nokogiri使用起来非常简单:require'nokogiri'require'open

ruby-on-rails - Ruby:我可以在类方法中使用实例方法吗?

我有一个包含此类方法的类:defself.get_event_record(row,participant)event=Event.where(:participant_id=>participant.id,:event_type_code=>row[:event_type],:event_start_date=>self.format_date(row[:event_start_date])).firstevent=Event.new(:participant_id=>participant.id,:event_type_code=>row[:event_type],:event_s

ruby - 如何使用 ruby​​ 有效地获取单词频率?

示例输入:"Iwas09809home--Yes!yes!Youwas"和输出:{'yes'=>2,'was'=>2,'i'=>1,'home'=>1,'you'=>1}我的代码不起作用:defget_words_f(myStr)myStr=myStr.downcase.scan(/\w/).to_s;h=Hash.new(0)myStr.split.eachdo|w|h[w]+=1endreturnh.to_a;endprintget_words_f('Iwas09809home--Yes!yes!Youwas'); 最佳答案 这

ruby - 将实例方法委托(delegate)给类方法

在Ruby中,假设我有一个类Foo允许我对我的大量Foos进行分类。所有Foos都是绿色和球形的是自然界的基本法则,因此我定义了类方法如下:classFoodefself.colour"green"enddefself.is_spherical?trueendend这让我做Foo.colour#"green"但不是my_foo=Foo.newmy_foo.colour#Error!尽管my_foo显然是绿色的。显然,我可以定义一个调用self.class.colour的实例方法colour,但如果我有很多这样的基本特征,那将变得笨拙。我大概也可以通过定义method_missing来尝

ruby - 获取调用者类

我在写Logger时遇到了自动添加类名的问题,我从中调用了print_log方法。例如这样的事情:classLoggerdefself.print_log(string)putsTime.now.strftime('%T|')+*caller_class_name_here*+'-'+stringendendclassMyClassdefinitializeLogger.print_log'called.new()method'endend作为调用MyClass.new方法的结果,我想在输出中看到:14:41:23|MyClass-called.new()method我确定可以使用ca

ruby - 如何在 Ruby 1.9 中只获取有序哈希的一个子集?

让我们举个例子:d={"a"=>1,"b"=>2,"c"=>3,"d"=>4}由于哈希现在是有序的,我可能想要从a到b或从c到d。问题是我无法执行d[0..1]或d[2..3]。不过我可以这样做:irb>d.to_a[0..1]=>[["a",1],["b",2]]...但这感觉很乱,我不想为这样的操作转换我的哈希值。是否有更清洁的解决方案来处理这个问题?#HolyGrailirb>d[0..1]=>{"a"=>1,"b"=>2}我可以看到如何为自己编写这样的方法,但可能已经有一些原生的东西我可以使用...? 最佳答案 你可以这样做

ruby-on-rails - 最佳实践 - 在 Ruby on Rails View 中传递实例变量或使用参数?

根据下面的例子,最佳实践是什么?案例一controller.rb...defindex...@group=params[:group]@team=params[:team]@org=params[:org]...endindex.html.haml=link_to@group,'#'=link_to@team,'#'=link_to@org,'#'案例2controller.rb...defindex......endindex.html.haml=link_toparams[:group],'#'=link_toparams[:team],'#'=link_toparams[:org

ruby - 如何获取已安装的具有 native 扩展的 gem 列表?

我在Windows上,已经从ruby​​1.8.x更新到1.9.x,现在收到错误弹出窗口,提示缺少ruby​​-mssomethingrt.1.8.x.dll。我想找出哪些gem具有native扩展,以便我可以卸载它们并在安装期间再次在本地强制重建native扩展,以使错误消失。 最佳答案 基于thisanswer,这里是一个解决方案,它可以找到并提供重新安装具有native扩展的gems适用于最近的ruby(>=1.9)。native_gems=[]Gem::Specification.eachdo|spec|native_gem